package engine;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Engine2
 */
@WebServlet("/Engine2")
public class Engine2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected Messenger msgr;
	protected Game game;
	protected Thread gameThread;
	protected Thread msgrThread;
	protected boolean gameStarted = false;
	protected boolean msgrStarted = false;


	public void endGame(){
		gameStarted = false;
		game.stop();
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("param");
		HttpSession session = request.getSession(true);
		//log("Session is: " + session.getId());
		//log("Action is: " + action);
		if (action.equals("isGameRunning")){
			PrintWriter pw = response.getWriter();
			if (this.gameStarted){
				pw.println("running = true");
			}
			else {
				pw.println("running = false");
			}
			pw.close();
		}
		
		if (action.equals("restore")){
			synchronized(game){
				game.notify();
			}
			//TODO:
			game.restoreState();
		}
		if (action.equals("start")) {
			//game.startGame(game);
			if (this.gameStarted){
				game.stop();
				//gameThread.stop();
				msgr.stop();
				//msgrThread.stop();
				this.gameStarted = false;
				this.msgrStarted = false;
			}
			msgr = new Messenger();
			msgrThread = new Thread(msgr,"[msgr]");
			//msgrThread.setDaemon(true);
			msgrThread.start();
			game = new Game(this);
			game.messenger = msgr;
			gameThread = new Thread(game,"[game]");
			gameThread.start();
			log("Game started");
			this.gameStarted = true;
			this.msgrStarted = true;
			
		}
		if (this.msgrStarted){
			if (action.equals("update")){
				response.setContentType("application/x-javascript");
				response.setCharacterEncoding("UTF-8");
				synchronized(msgr){
					msgr.isUpdating = true;
					msgr.notify();
					msgr.getUpdate(response);
				}
			}
		}
		if (this.gameStarted){
			if (action.equals("fold")) {
				synchronized(game){
					game.notify();
				}
				game.human.fold();
				log("Player folds");
				
			}
			else if (action.equals("raise")){
				synchronized(game){
					game.notify();
				}
				game.human.raise();
				log("Player raises");
			}
			else if (action.equals("check")){
				synchronized(game){
					game.notify();
				}
				game.human.checkCall();
				log("Players check/calls");
			}
			else if (action.equals("leave")){
				game.stop();
				msgr.stop();
				this.gameStarted = false;
				this.msgrStarted = false;
				log("Player leaves");
			}
			else if (action.equals("msg")){
				System.out.println(request.getParameter("message"));
				
			}
			
			PrintWriter pw = response.getWriter();
			if (this.gameStarted){
				pw.println("OK");
			}
			else {
				pw.println("OK");
			}
			pw.close();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
